package com.abvnet.project.water.project.service;

import com.abvnet.common.utils.DateUtils;
import com.abvnet.common.utils.security.ShiroUtils;
import com.abvnet.common.utils.text.Convert;
import com.abvnet.project.system.dict.domain.DictData;
import com.abvnet.project.system.dict.mapper.DictDataMapper;
import com.abvnet.project.water.project.domain.*;
import com.abvnet.project.water.project.mapper.*;
import com.google.common.collect.Lists;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.List;
import java.util.stream.Collectors;

import static java.math.RoundingMode.HALF_UP;

/**
 * 项目 服务层实现
 * 
 * @author duj
 * @date 2019-07-02
 */
@Service
public class ProjectInfoServiceImpl implements IProjectInfoService {
	@Autowired
	private ProjectInfoMapper projectInfoMapper;

    @Autowired
    private ProjectFlowMapper projectFlowMapper;

    @Autowired
    private ProjectFlowSewageMapper projectFlowSewageMapper;

    @Autowired
    private ProjectTechMapper projectTechMapper;

    @Autowired
    private ProjectEqpMapper projectEqpMapper;

    @Autowired
    private ProjectWaterMapper projectWaterMapper;

    @Autowired
    private ProjectMicroMapper projectMicroMapper;

    @Autowired
    private ProjectPharmMapper projectPharmMapper;
    
    @Autowired
    private ProjectProgressMapper projectProgressMapper;

    @Autowired
    private ProjectCustomerEvalMapper projectCustomerEvalMapper;

    @Autowired
    private ProjectCustomerEvaluateMapper projectCustomerEvaluateMapper;

    @Autowired
    private ProjectTestMapper projectTestMapper;

    @Autowired
    private DictDataMapper dictDataMapper;

    @Autowired
    private ProjectCaseMapper projectCaseMapper;

    @Autowired
    private ProjectPartnerMapper projectPartnerMapper;

    @Autowired
    private ProjectCommentMapper projectCommentMapper;

	/**
     * 查询项目信息
     * 
     * @param projectId 项目ID
     * @return 项目信息
     */
    @Override
	public ProjectInfo selectProjectInfoById(Long projectId) {
	    return projectInfoMapper.selectProjectInfoById(projectId);
	}

    @Override
    public ProjectInfo selectDiagnosisByProjectId(Long projectId) {
        return projectInfoMapper.selectDiagnosisByProjectId(projectId);
    }

    @Override
    public ProjectInfo selectSummaryByProjectId(Long projectId) {
        return projectInfoMapper.selectSummaryByProjectId(projectId);
    }

    @Override
    public ProjectInfo selectProgressCodeById(Long projectId) {
        return projectInfoMapper.selectProgressCodeById(projectId);
    }

    /**
     * 查询项目列表
     * 
     * @param projectInfo 项目信息
     * @return 项目集合
     */
	@Override
	public List<ProjectInfo> selectProjectInfoList(ProjectInfo projectInfo) {
	    return projectInfoMapper.selectProjectInfoList(projectInfo);
	}

    @Override
    public List<ProjectInfo> selectCustomerProjectInfoList(ProjectInfo projectInfo) {
        return projectInfoMapper.selectCustomerProjectInfoList(projectInfo);
    }

    @Override
    public List<ProjectInfo> selectListForApi(ProjectInfo projectInfo) {
        return projectInfoMapper.selectListForApi(projectInfo);
    }

    @Override
    public List<ProjectInfo> selectMapList() {
        return projectInfoMapper.selectMapList();
    }

    @Override
    public List<PieChart> selectAreaList() {
        return projectInfoMapper.selectAreaList();
    }

    @Override
    public RateBar selectRepRateList() {
        List<RepData> repData = projectInfoMapper.selectRateList();
        RateBar rb = new RateBar();
        if (CollectionUtils.isNotEmpty(repData)) {
            List<Integer> year1List = Lists.newArrayList();
            List<Integer> rate1List = Lists.newArrayList();
            List<String> year2List = Lists.newArrayList();
            List<Integer> rate2List = Lists.newArrayList();
            for (RepData rd : repData) {
                year1List.add(rd.getProjectYear());
                rate1List.add(BigDecimal.valueOf(rd.getRepNum()*100).divide(BigDecimal.valueOf(rd.getTotalNum()),
                        HALF_UP).intValue());
            }
            rb.setYear1List(year1List);
            rb.setRate1List(rate1List);
            if (repData.size() > 1) {
                for (RepData rd : repData) {
                    int year = rd.getProjectYear();
                    for (RepData r : repData) {
                        if ((year + 1) == r.getProjectYear().intValue()) {
                            year2List.add(new StringBuilder().append(year).append("-").append(r.getProjectYear()).toString());
                            rate2List.add(BigDecimal.valueOf((rd.getRepNum() + r.getRepNum())*100).divide(BigDecimal.valueOf((rd.getTotalNum() + r.getTotalNum())),
                                    HALF_UP).intValue());
                        }
                    }
                }
                rb.setYear2List(year2List);
                rb.setRate2List(rate2List);
            }
        }
        return rb;
    }

    @Override
    public List<PieChart> selectIndustryList() {
        return projectInfoMapper.selectIndustryList();
    }

    @Override
    public List<ProjectInfo> selectProjectListByPlanId(Long planId) {
        return projectInfoMapper.selectProjectListByPlanId(planId);
    }

    @Override
    public List<ProjectInfo> selectUnProjectListByPlanId(Long planId) {
        return projectInfoMapper.selectUnProjectListByPlanId(planId);
    }

    /**
     * 新增项目
     * 
     * @param projectInfo 项目信息
     * @return 结果
     */
	@Override
    @Transactional
	public int insertProjectInfo(ProjectInfo projectInfo) {
        projectInfo.setCreateBy(ShiroUtils.getLoginName());
        projectInfo.setProjectYear(Integer.valueOf(DateUtils.getYear()));
        int rows = projectInfoMapper.insertProjectInfo(projectInfo);
        initProjectFlow(projectInfo);
        initProjectTech(projectInfo);
        initProjectEqp(projectInfo);
        initProjectWater(projectInfo);
        initProjectMicro(projectInfo);
        initProjectPharm(projectInfo);
        initProjectProgress(projectInfo);
        initProjectEval(projectInfo);
        initProjectTest(projectInfo);
        initProjectEvaluate(projectInfo);
        initProjectCase(projectInfo);
        return rows;
	}

    /**
     * 从手机端新增项目
     *
     * @param projectInfo 项目信息
     * @return 结果
     */
    @Override
    @Transactional
    public int insertProjectInfoForApp(ProjectInfo projectInfo) {
        projectInfo.setProjectYear(Integer.valueOf(DateUtils.getYear()));
        int rows = projectInfoMapper.insertProjectInfo(projectInfo);
        initProjectFlow(projectInfo);
        initProjectTech(projectInfo);
        initProjectEqp(projectInfo);
        initProjectWater(projectInfo);
        initProjectMicro(projectInfo);
        initProjectPharm(projectInfo);
        initProjectProgress(projectInfo);
        initProjectEval(projectInfo);
        initProjectTest(projectInfo);
        initProjectEvaluate(projectInfo);
        initProjectCase(projectInfo);
        return rows;
    }

	public void initProjectFlow(ProjectInfo projectInfo) {
	    ProjectFlow pf = new ProjectFlow();
	    pf.setProjectId(projectInfo.getProjectId());
        projectFlowMapper.insertProjectFlow(pf);
    }

	public void initProjectTech(ProjectInfo projectInfo) {
        ProjectTech pt = new ProjectTech();
        pt.setProjectId(projectInfo.getProjectId());
        projectTechMapper.insertProjectTech(pt);
    }

    public void initProjectEqp(ProjectInfo projectInfo) {
        ProjectEqp pe = new ProjectEqp();
        pe.setProjectId(projectInfo.getProjectId());
        projectEqpMapper.insertProjectEqp(pe);
    }

    public void initProjectWater(ProjectInfo projectInfo) {
        ProjectWater pw = new ProjectWater();
        pw.setProjectId(projectInfo.getProjectId());
        projectWaterMapper.insertProjectWater(pw);
    }

    public void initProjectMicro(ProjectInfo projectInfo) {
        ProjectMicro pm = new ProjectMicro();
        pm.setProjectId(projectInfo.getProjectId());
        projectMicroMapper.insertProjectMicro(pm);
    }

    public void initProjectPharm(ProjectInfo projectInfo) {
        ProjectPharm pp = new ProjectPharm();
        pp.setProjectId(projectInfo.getProjectId());
        projectPharmMapper.insertProjectPharm(pp);
    }
    
    public void initProjectProgress(ProjectInfo projectInfo) {
        List<DictData> list = dictDataMapper.selectDictDataByType("project_progress");
        List<ProjectProgress> ppList = list.stream().map(dictData ->
                ProjectProgress.builder().progressCode(dictData.getDictValue()).progressName(dictData.getDictLabel()).projectId(projectInfo.getProjectId()).build()
        ).collect(Collectors.toList());
        projectProgressMapper.batchProjectProgress(ppList);
    }

    public void initProjectEval(ProjectInfo projectInfo) {
	    projectCustomerEvalMapper.insertProjectCustomerEval(ProjectCustomerEval.builder().projectId(projectInfo.getProjectId()).build());
    }

    public void initProjectTest(ProjectInfo projectInfo) {
        ProjectTest pt = new ProjectTest();
        pt.setProjectId(projectInfo.getProjectId());
        projectTestMapper.insertProjectTest(pt);
    }

    public void initProjectEvaluate(ProjectInfo projectInfo) {
        ProjectCustomerEvaluate pce = new ProjectCustomerEvaluate();
        pce.setProjectId(projectInfo.getProjectId());
        pce.setProjectPrice(projectInfo.getProjectPrice());
        projectCustomerEvaluateMapper.insertProjectCustomerEvaluate(pce);
    }

    public void initProjectCase(ProjectInfo projectInfo) {
	    ProjectCase pc = new ProjectCase();
	    pc.setProjectId(projectInfo.getProjectId());
        projectCaseMapper.insertProjectCase(pc);
    }

	/**
     * 修改项目
     * 
     * @param projectInfo 项目信息
     * @return 结果
     */
	@Override
	public int updateProjectInfo(ProjectInfo projectInfo) {
	    projectInfo.setUpdateBy(ShiroUtils.getLoginName());
	    return projectInfoMapper.updateProjectInfo(projectInfo);
	}

    @Override
    public int updateProjectInfoForApi(ProjectInfo projectInfo) {
        return projectInfoMapper.updateProjectInfo(projectInfo);
    }

    /**
     * 删除项目对象
     * 
     * @param ids 需要删除的数据ID
     * @return 结果
     */
	@Override
	public int deleteProjectInfoByIds(String ids) {
        Long[] pids = Convert.toLongArray(ids);
        for (Long id : pids) {
            projectFlowMapper.deleteProjectFlowById(id);
            projectFlowSewageMapper.deleteProjectFlowSewageById(id);
            projectTechMapper.deleteByProjectId(id);
            projectEqpMapper.deleteByProjectId(id);
            projectWaterMapper.deleteByProjectId(id);
            projectMicroMapper.deleteByProjectId(id);
            projectPharmMapper.deleteByProjectId(id);
            projectProgressMapper.deleteByProjectId(id);
            projectCustomerEvalMapper.deleteByProjectId(id);
            projectTestMapper.deleteByProjectId(id);
            projectCustomerEvaluateMapper.deleteByProjectId(id);
            projectCaseMapper.deleteByProjectId(id);
            projectPartnerMapper.deleteByProjectId(id);
            projectCommentMapper.deleteByProjectId(id);
        }
		return projectInfoMapper.deleteProjectInfoByIds(Convert.toStrArray(ids));
	}
	
}